import React from 'react'
import PropTypes from 'prop-types'
import {Modal, Form, Input, Select, Avatar, Button, Tree, message} from 'antd';
import axios from "axios";
import api from "../../../../api/api";
import MenuTree from "./MenuTree"

const {Option} = Select;
const FormItem = Form.Item;

@Form.create()
class Model extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            visible: false,
            treeData: [],
            permissionId: '',
            selectedMenuList: []
        };
    }

    componentDidMount() {
        console.log("dialog初始化==", this.props);
        this.props.onRef(this)
        // const { setBaseInfo } = this
    }

    // setBaseInfo = () => {
    //     let data = this.props.data.gridList
    //     const { form } = this.props;
    //     Object.keys(form.getFieldsValue()).forEach(key => {
    //         const obj = {};
    //         if (data[key]) {
    //             obj[key] = data[key] || null;
    //         }
    //         form.setFieldsValue(obj);
    //     });
    // };
    //清空form表单数据
    clearData() {
        this.props.form.resetFields();
        this.child.setItemsValue([])
        this.setState({
            permissionId: ''
        })
    }

    //初始化编辑数据
    // initEditData = () => {
    //     this.setBaseInfo()
    // }

    handleOk = () => {
        let that = this;
        this.props.form.validateFields((error, values) => {
            if (error != null) {
                return;
            }
            let {param} = this.state
            console.log(this.child)
            console.log("formRef", this.child.getItemsValue());
            console.log("values", values.authorityName)

            let url = api.api_URL + "/admin/authority/addAuthorityInfo";
            let data = {
                "authorityName": values.authorityName,
                "treeMenuArray": this.child.getItemsValue(),
                "orgCode": localStorage.getItem('orgCode')
            };
            if (this.state.permissionId) {
                //更新
                url = api.api_URL + "/admin/authority/updateAuthorityInfo";
                data = {
                    "authorityName": values.authorityName,
                    "treeMenuArray": this.child.getItemsValue(),
                    "authorityId": this.state.permissionId,
                    "orgCode": localStorage.getItem('orgCode')
                };
            }
            axios.post(url, data, {
                headers: {
                    "Authorization": localStorage.getItem('token'),
                    "Content-Type": api.ContentType
                }
            }).then(function (res) {
                console.log(res.data)
                if (res.data.code != 1) {
                    message.error({content:"失败，请重新尝试！"})
                    return;
                }
                message.success({content:"成功！"})

                that.handleCancel()
            }).catch(function (err) {
                message.error({content:"失败，请重新尝试！"})
                console.log(err);
            });
        })
    }

    handleCancel = e => {
        console.log(e);
        this.clearData();
        this.props.handleCancel()
    };

    getPermissionSetting(permissionId) {
        var that = this;
        console.info("获取权限信息permissionId=" + permissionId);
        let url = api.api_URL + "/admin/authority/queryAuthorityInfoById?authorityInfoId=" + permissionId + "&orgCode=" + localStorage.getItem('orgCode');
        axios.get(url, {
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": api.ContentType
            }
        }).then(function (res) {
            if (res.data.code != 1) {
                message.error({content:"获取权限信息失败，请重新尝试！"})
                return;
            }
            let authorityInfo = res.data.data.authorityInfo;
            that.props.form.setFieldsValue({
                "authorityName": authorityInfo.authorityName
            })
            that.setState({
                "permissionId": permissionId
            })
            that.child.getMenuTree(authorityInfo.treeMenuArray)
        })
    }

    render() {
        const {getFieldDecorator} = this.props.form
        return (
            <div>
                <Modal
                    title={this.props.data.title}
                    visible={this.props.data.visible}
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                >
                    <Form name="control-ref"
                          labelCol={{span: 6}}
                          wrapperCol={{span: 14}}
                          layout="horizontal"
                    >
                        <FormItem label="权限组名称：" name="authorityName">
                            {getFieldDecorator("authorityName", {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入权限组名称"
                                    }
                                ]
                            })(
                                <Input/>
                            )}
                        </FormItem>
                        <FormItem label="选择菜单：" name="authorityTrees">
                            <MenuTree onRef={(ref) => {
                                this.child = ref;
                            }}/>
                        </FormItem>
                    </Form>
                </Modal>
            </div>
        );
    }

}

Model.propTypes = {
    type: PropTypes.string,
    item: PropTypes.object,
    onOk: PropTypes.func,
}
export default Model